<template>
  <div class="comment-list">
    <!-- 列表头部 -->
    <van-cell class="comment-header" center border>
      <div slot="title">全部评论 ({{ total }})</div>
      <div slot="default">6 点赞</div>
    </van-cell>
    <!-- 列表项 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <div
        class="comment-wrap"
        v-for="(comment, index) in commentList"
        :key="index"
      >
        <van-cell :border="false" class="comment-item">
          <!-- <div class="image" slot="icon">
               
            </div> -->
          <div slot="title" class="title">
            <van-image fit="cover" round :src="comment.aut_photo" />
            <span class="user-name">{{ comment.aut_name }}</span>
          </div>
          <div class="comment-right">
            <span class="like-count">{{ comment.like_count }}</span>
            <van-icon color="#000" name="good-job-o" />
          </div>
        </van-cell>
        <div class="comment-content">
          <div class="comment-detail">{{ comment.content }}</div>
          <div class="comment-repay">
            <van-button 
               color="#f7f8fa" 
               type="primary" 
               round 
               size="small"
               @click="getCommentInfo(comment)"
              >{{ comment.reply_count == 0 ? "" : comment.reply_count }} 回复
              ></van-button
            >
            <span class="comment-time">{{
              comment.pubdate | relativeTime
            }}</span>
          </div>
        </div>
      </div>
    </van-list>


    <!-- 评论详情/评论回复 -->
    <van-popup 
      class="comment-popup"
      v-model="isShowReplaycomment" 
      position="right" 
      :style="{ height: '100%', width:'100%' }">
       <van-cell :border="false">
         <div slot="title" class="user-info"> 
           <van-image
              round
              width="40"
              height="40"
              :src="currentComment.aut_photo"
            />
            <span class="title">{{ currentComment.aut_name }}</span>
         </div>
         
         <div v-if="currentComment.is_followed" >已关注</div>
         <div v-else>关注</div>
       </van-cell>
      <div class="content">
           <div class="conment-detail">{{ currentComment.content }}</div>
      </div>
       <div class="other">
         <span>{{ currentComment.pubdate | relativeTime }}</span>
         <div class="like">
           <span>{{ currentComment.like_count }}</span>
           <van-icon name="good-job-o" />
         </div>
       </div>
       <!-- 全部评论回复 -->
       <div  class="allReplay-content">
          <van-cell :border="false">
            <div slot="title">全部回复</div>
            <div>
              <van-button round size="small" type="info">去回复</van-button>
            </div>
          </van-cell>
          <div v-for=" (replayComment,index) in allReplayComment" :key="index">
          <van-cell :border="false">
         <div slot="title" class="user-info"> 
           <van-image
              round
              width="40"
              height="40"
              :src="replayComment.aut_photo"
            />
            <span class="title">{{ replayComment.aut_name }}</span>
         </div>
         
         <div v-if="replayComment.is_followed" >已关注</div>
         <div v-else>关注</div>
       </van-cell>
          <div class="content">
           <div class="conment-detail">{{ replayComment.content }}</div>
      </div>
       <div class="other" style="border-bottom:unset">
         <span>{{ replayComment.pubdate | relativeTime }}</span>
         <div class="like">
           <span>{{ replayComment.like_count }}</span>
           <van-icon name="good-job-o" />
         </div>
       </div>
        </div>
        </div>
      </van-popup>

    <!-- 评论详情/评论回复 -->

    <!-- 回复评论 -->

    <!-- 回复评论 -->
  </div>
</template>

<script>
import { getArticleComment,getReplayComment } from "@/api/comment";
import bus from '@/utils/bus'

export default {
  name: "Commentlist",
  props: {
    articleInfo: {
      type: Object,
      require: true,
    },
  },
  data() {
    return {
      articleId: "",
      offset: "",
      limit: 10,
      commentList: [],
      total: 0,
      loading: false,
      finished: false,
      isShowReplaycomment: false,
      currentComment: {},
      allReplayComment: []
    };
  },
//   异步获取 articleInfo 可能会获取不到，这里采用watch监听数据是否已经传递过来再执行
  watch: {
    articleInfo(newValue, oldValue) {
      console.log("123")
      if (newValue.art_id) {
        this.articleId = newValue.art_id;
        newValue && this.onLoad ();
      }
    },
  },
  mounted() {
      bus.$on('reloadComment', () =>{
          this.commentList = []
          this.offset = ''
          this.loading = false
          this.onLoad()
      })
  },

  methods: {
    // 获取文章评论
    // async loadCommentList() {
      
    // },
   async onLoad () {
        if (this.articleId) {
            const {data: { data },} = await getArticleComment({
                type: "a",
                source: this.articleId,
                offset: this.offset || "",
                limit: this.limit,
            });
            console.log(data);
            this.total = data.total_count;
            this.offset = data.last_id;
            this.commentList.push(...data.results)
            // 加载状态结束
                this.loading = false;
            // 数据全部加载完成
            if (data.results.length === 0) {
                this.finished = true;
            }
        }
    },
    async getCommentInfo (comment) {
      this.isShowReplaycomment = true
      this.currentComment = comment
      const { data:{data} } = await getReplayComment({
        type: 'c',
        source: comment.com_id,
        limit: this.limit
      })
      this.allReplayComment.push(...data.results)
      console.log(data)
    }
  },
};
</script>

<style lang="less" scoped>
.comment-list {
  .comment-header {
    border-bottom: 2px solid #f7f8fa;
  }
  .comment-item {
    /deep/.van-image {
      height: 40px;
      width: 40px;
      display: flex;
    }
    .title {
      font-size: 16px;
      display: flex;
      align-items: center;
      .user-name {
        margin-left: 10px;
      }
    }
    .comment-right {
      /deep/.van-icon {
        font-size: 20px;
      }
      .like-count {
        margin-right: 5px;
      }
    }
    /deep/.van-cell__label {
      width: 100%;
    }
  }
  .comment-content {
    padding: 0 0.42667rem;
    .comment-detail {
      font-size: 0.42667rem;
      color: #666;
      word-break: break-all;
      margin-left: 50px;
    }
    .comment-repay {
      margin-top: 10px;
      display: flex;
      align-items: center;
      margin-left: 50px;
      .van-button__text {
        color: #000;
      }
      .comment-time {
        color: #999;
        font-size: 0.32rem;
        margin-left: 0.26667rem;
      }
    }
  }

  .comment-popup{
    margin-top: 46px;
    .user-info {
      display: flex;
      align-items: center;
      .title {
        margin-left: 10px;
      }
      
    }
    .other,.content{
      // width: 100%;
      display: flex;
      padding: 0 17px;
      padding-left: 67px;
      justify-content: space-between;
      align-items: center;
      color: #969799;
      font-size: 14px;
      .conment-detail{
      word-break: break-all;
      padding: 0;
      }
    }
    .other {
      margin-top: 10px;
      padding-bottom: 20px;
      border-bottom: 1px solid #ccc;
      .van-icon-good-job-o{
        color: #999;
        font-size: 20px;
        margin-left: 3px;
      }
    }
    .allReplay-content{
      position: fixed;
      left: 0;
      right: 0;
      top: 128px;
      overflow-y: auto;
    }
  }
}
</style>